package com.afkt.project.ui.slice;

import com.afkt.project.util.LogUtils;
import com.dev.utils.LogPrintUtils;
import com.dev.utils.common.DevCommonUtils;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import utils_use.logger.LogTools;
import utils_use.logger.TestData;

public class DevLoggerSlice extends AbilitySlice {

    private Button goBack;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_dev_logger);

        /**
         *设置返回
         * */
        goBack =(Button) findComponentById(ResourceTable.Id_btn_crash_back);
        if(goBack!=null){
            goBack.setClickedListener(component -> {
               onBackPressed();
            });
        }


        /**
         *
         * 打印日志
         * */
        Button printLog = (Button) findComponentById(ResourceTable.Id_print_log);
        if(printLog != null){
            printLog.setClickedListener(component -> {
                // = 打印零散数据 =
                TestData.ShareMsgVo sMsgVo = new TestData.ShareMsgVo();
                sMsgVo.sTitle = "分享 Blog";
                sMsgVo.sText = null;
                sMsgVo.sImagePath = "http://t.jpg";
                sMsgVo.sTitleUrl = "http://www.test.com";

                TestData.UserInfoVo uInfoVo = new TestData.UserInfoVo();
                uInfoVo.uName = "BlogRecord";
                uInfoVo.uPwd = "log_pwd";
                uInfoVo.uAge = 100;

                // 打印分享数据
                LogPrintUtils.d(LogTools.getShareMsgVoData(sMsgVo));
                // 打印用户数据
                LogPrintUtils.d(LogTools.getUserInfoVoData(uInfoVo));
                // 打印零散数据
                LogPrintUtils.d(LogTools.getScatteredData(uInfoVo.uName, sMsgVo.sTitle, uInfoVo.uAge));

                // = 打印测试数据 =
                // 日志 TAG
                final String tag = "LogPrint";
                // = 使用 BaseApplication 默认配置 =
                // JSON 数组
                LogPrintUtils.json("[" + TestData.JSON_WITH_NO_LINE_BREAK + "," + TestData.JSON_WITH_NO_LINE_BREAK + "]");
                // JSON 对象
                LogPrintUtils.json(TestData.SMALL_SON_WITH_NO_LINE_BREAK);
                // XML 数据
                LogPrintUtils.xml(TestData.XML_DATA);
                // = 其他 =
                LogPrintUtils.v("测试数据 - v");
                LogPrintUtils.d("测试数据 - d");
                LogPrintUtils.i("测试数据 - i");
                LogPrintUtils.w("测试数据 - w");
                LogPrintUtils.e("错误 - e");
                LogPrintUtils.wtf("测试数据 - wtf");
                // =
                LogPrintUtils.vTag(tag, "测试数据 - v");
                LogPrintUtils.vTag(tag, "测试数据 - d");
                try {
                    Class clazz = Class.forName("asdfasd");
                } catch (ClassNotFoundException e) {
                    LogPrintUtils.e(e, "发生异常");
                }
                // 占位符 ( 其他类型, 一样 )
                LogPrintUtils.d("%s 测试占位符数据 d%s", "1.", "Format");
                // =
                LogPrintUtils.dTag(tag, "%s 测试占位符数据 d%s", "1.", "Format");

                new ToastDialog(getContext()).setText("日志已打印到HiLog上！").show();
            });
        }


        /**
         *
         * 打印日志耗时
         * */
        Button LogCostTime = (Button) findComponentById(ResourceTable.Id_print_log_time);
        if(printLog != null){
            LogCostTime.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {

                    new ToastDialog(getContext()).setText("日志耗时已打印到HiLog上！").show();

                    // 拼接字符串
                    StringBuilder builder = new StringBuilder();
                    // 日志 TAG
                    final String tag = "CALC_TIME";
                    // =
                    // 遍历次数
                    int count = 100;
                    // 设置开始时间
                    long sTime = System.currentTimeMillis();
                    // 开始遍历
                    for (int i = 0; i < count; i++) {
                        LogUtils.debug(tag, "A: " + (i + 1));
                    }
                    // 拼接时间信息
                    DevCommonUtils.timeRecord(builder, "正常系统 Log 耗时记录", sTime, System.currentTimeMillis());

                    // =
                    // 设置开始时间
                    sTime = System.currentTimeMillis();
                    // 开始遍历
                    for (int i = 0; i < count; i++) {
                        // DevLogger.d("B: %s", (i + 1));
                        LogPrintUtils.dTag(tag, "B: %s", (i + 1));
                    }
                    // 拼接时间信息
                    DevCommonUtils.timeRecord(builder, "\nLogger 耗时记录", sTime, System.currentTimeMillis());

                    // 拼接时间信息
                    DevCommonUtils.timeRecord(builder, "\nLogger 耗时记录 - 使用自定义日志配置", sTime, System.currentTimeMillis());
                    // 打印次数
                    builder.append("\n\n打印次数: ").append(count);
                    // 打印耗时信息
                    LogPrintUtils.dTag("print_time", builder.toString());

                }
            });
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }


    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}


